package com.qf.controller.business;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.RespBean;
import com.qf.entitys.Specifications;
import com.qf.service.SpecificationsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@Controller
@RequestMapping("/business/specifications")
public class SpecificationsController {
    @Resource
    SpecificationsService service;

    @GetMapping("/list")
    public String list(Integer pageindex,HttpServletRequest request,Specifications specifications){
        PageInfo<Specifications> specificationsPageInfo = service.selectAll(pageindex,specifications);
        request.setAttribute("pageInfo",specificationsPageInfo);
        return "business/specificationslist";
    }
    @GetMapping("/add")
    public String add(){
        return "business/specificationsAdd";
    }
    @PostMapping("/add")
    public String add(HttpServletRequest request,Specifications specifications){
        int i = service.insertSelective(specifications);
        if (i>0){
            return "redirect:list?pageindex=1";
        }else {
            return "manager/systemerror";
        }
    }

    @GetMapping("/update")
    public String update(Integer id,HttpServletRequest request){
        Specifications specifications = service.selectByPrimaryKey(id);
        request.setAttribute("sp",specifications);
        return "business/specificationsUpdate";
    }

    @PostMapping("/update")
    @ResponseBody
    public RespBean update(Specifications specifications,HttpServletRequest request) throws IOException {

        int i = service.updateByPrimaryKeySelective(specifications);
        if (i>0){
            return RespBean.success("修改成功，即将返回列表!");
        }else {
            return RespBean.error("修改失败!");
        }
    }
    @GetMapping("/del")
    @ResponseBody
    public RespBean del(Integer id,Specifications specifications){
        try {
            int i = service.deleteByPrimaryKey(id);
            if (i>0){
                return RespBean.success("删除成功");
            }else {
                return RespBean.error("删除失败");
            }
        }catch (Exception ex){
            return RespBean.error("删除失败");
        }
    }
    @GetMapping("/delbyids")
    @ResponseBody
    public RespBean delbyids(@RequestParam("ids[]")Integer[] ids){
        try {
            int i = service.deleteByIds(ids);
            if (i>0){
                return RespBean.success("批量删除成功！");
            }else {
                return RespBean.error("批量删除失败！");
            }
        }catch (Exception ex){
            return RespBean.error("批量删除失败！");
        }
    }


}
